import React from 'react';
import ReactDom from 'react-dom';
import { Router, hashHistory, browserHistory } from 'react-router';
import './index.scss';
import 'css/fonts.css';
import playerStore from 'js/store/playerStore';
console.log('index')
import gameStore from 'js/store/gameStore';
import eventHandle from 'js/core/eventHandle';
import { Provider, observer } from 'mobx-react';
import { api } from 'js/api/api';
import { observable, action } from 'mobx'

const routes = [
    {
        path: '/init',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/index/init').default)
            }, 'init')
        },
        childRoutes: [
            {
                path: '/login',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/login/login').Login)
                    }, 'login')
                }
            },
            {
                path: '/register',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/login/login').Register)
                    }, 'register')
                }
            },
            {
                path: '/playerSelect',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/playerSelect/playerSelect').default)
                    }, 'playerSelect')
                }
            },
            {
                path: '/createPlayer',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/playerSelect/createPlayer').default)
                    }, 'createPlayer')
                }
            },
        ]
    },
    {
        path: '/battle',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/maps/maze/battle').default)
            }, 'battle')
        }
    },
    {
        path: '/bag',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/modal/bag/bag').default)
            }, 'bag')
        }
    },
    {
        path: '/battleData',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/maps/battleData/battleData').default)
            }, 'battleData')
        }
    },
    {
        path: '/main',
        getComponent: (nextState, cb) => {
            require.ensure([], (require) => {
                cb(null, require('views/main/main').default)
            }, 'main')
        },
        childRoutes: [
            {
                path: '/home',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/home/home').default)
                    }, 'home')
                }
            },
            {
                path: '/map',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/map/map').default)
                    }, 'map')
                }
            },
            {
                path: '/maze',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/maze/maze').default)
                    }, 'maze')
                }
            },
            {
                path: '/town',
                getComponent: (nextState, cb) => {
                    require.ensure([], (require) => {
                        cb(null, require('views/maps/town/town').default)
                    }, 'town')
                }
            },
        ]
    }
];


gameStore.setData(parseInt(window.localStorage.getItem('gameTime') || 0));
setInterval(() => {
    let data = gameStore.gameTime + 1;
    gameStore.setData(data);
    window.gameTime = data;
    window.localStorage.setItem('gameTime', data)
}, 5000);



api('getErrMap', (err, errMapData) => {
    window.errMap = errMapData;
    api('getPropName', (err, propNameData) => {
        window.propName = propNameData;
        api('getTotalConfig', (err, totalConfigData) => {
            window.totalConfig = totalConfigData;
            api('selectPlayer', {}, (err, playerData) => {
                if (err === 0) {
                    playerStore.setPlayer(playerData);
                    ReactDom.render(
                        <Provider gameStore={gameStore} playerStore={playerStore}><Router history={browserHistory} routes={routes} /></Provider>
                        , document.getElementById('app'))
                }
            });
        })
    });
});

